//
//  OnlineViewController.m
//  CaiPiao
//
//  Created by zhoup on 16/5/25.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "OnlineViewController.h"

@interface OnlineViewController ()

@property(nonatomic,strong) UITextField *textField;

@property(nonatomic,strong) UIDatePicker *datePicker;

@property(nonatomic,strong) UIToolbar *toolbar;


@property(nonatomic,strong) NSIndexPath *indexPath;
@end

@implementation OnlineViewController

-(UITextField *)textField
{
    if (_textField == nil) {
        _textField = [[UITextField alloc]init];
        _textField.bounds = CGRectMake(0, 0, self.view.width, 44);
        
        _textField.inputView = self.datePicker;
        _textField.inputAccessoryView = self.toolbar;
    }
    return _textField;
}


-(UIDatePicker *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
//        _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        
        _datePicker.locale =[NSLocale  localeWithLocaleIdentifier:@"zh-Hans"];

        _datePicker.datePickerMode = UIDatePickerModeTime;
    }
    
    return _datePicker;
}

-(UIToolbar *)toolbar{
    if (_toolbar == nil) {
        _toolbar = [[UIToolbar alloc]init];
        _toolbar.height = 44;
        
        UIBarButtonItem *confirm = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(conform:)];
        
        UIBarButtonItem *middle = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
         UIBarButtonItem *cancel = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
       // ddddddddd
        
     // ddddddddd
        _toolbar.items = @[cancel,middle,confirm];
    }
    return _toolbar;
}

#pragma mark - UIBarButtonItem
-(void)conform:(UIBarButtonItem *)sender
{
    
    NSLog(@"%@",self.datePicker.date);
    
    NSDate *date = self.datePicker.date;
    
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    
    formater.dateFormat = @"HH:mm";
    
    NSString *selectDate = [formater stringFromDate:date];
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
    
    UILabel *label = (UILabel *) cell.accessoryView;
    label.text =selectDate;
    [label sizeToFit];
    
    [self.textField resignFirstResponder];
    [self.textField removeFromSuperview];

}

-(void)cancel:(UIBarButtonItem *)sender
{
    [self.textField resignFirstResponder];
    [self.textField removeFromSuperview];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.indexPath = indexPath;
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [cell.contentView addSubview:self.textField];
    
    [self.textField becomeFirstResponder];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
//    [self.textField resignFirstResponder];
//    [self.textField removeFromSuperview];

}
@end
